package com.you.concurrent.threadpool;

import java.util.concurrent.*;

public class ThreadPoolExecutorDemo {

    public static void main(String[] args) {
        TaskRunnable taskRunnable = new TaskRunnable();

        //方式1
//        ExecutorService executorService = Executors.newFixedThreadPool(3);
//        executorService.execute(taskRunnable);

        //方式2

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
                5,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(10),
                new ExceptionProcess());
        for (int i = 0; i < 50; i++) {
            threadPoolExecutor.execute(taskRunnable);
        }

    }

    static class ExceptionProcess implements RejectedExecutionHandler {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            throw new RejectedExecutionException("-----异常----" + r);
        }
    }

    static class TaskRunnable implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-----test thread pool runtask!");
        }
    }
}
